package com.sgb.kjwl.view.ui.adapter.self.offer;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.harisucici.jetpackbase.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.TruckApplyEntity;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.customview.WSTextView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.StringUtil;

import java.util.List;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/4/7
 */
public class TruckApplyAdapter extends BaseRecycleAdapter<TruckApplyEntity> {
    private boolean isisGeneralGoods = true;

    public TruckApplyAdapter(Context context, List<TruckApplyEntity> data) {
        super(context, R.layout.truck_apply_list_item, data);
        // this.isisGeneralGoods = isisGeneralGoods;
    }

    public void setIsisGeneralGoods(boolean isisGeneralGoods) {
        this.isisGeneralGoods = isisGeneralGoods;
        notifyDataSetChanged();
    }

    @Override
    public void convert(ItemViewHolder holder, TruckApplyEntity entity) {
        holder.setBinding(BR.entity, entity);
        TextView name = holder.getView(R.id.name);
        TextView car_num = holder.getView(R.id.car_num);
        WSTextView route_address = holder.getView(R.id.route_address);
        TextView haul = holder.getView(R.id.haul);
        TextView goods_details = holder.getView(R.id.goods_details);
        WSTextView freight_unit = holder.getView(R.id.freight_unit);
        WSTextView freight = holder.getView(R.id.freight);
        TextView phone_num = holder.getView(R.id.phone_num);
        TextView freight_offer = holder.getView(R.id.freight_offer);
        TextView freight_all = holder.getView(R.id.freight_all);
        TextView car_type = holder.getView(R.id.car_type);
        TextView car_type_hint = holder.getView(R.id.car_type_hint);
        TextView offer_time = holder.getView(R.id.offer_time);
        ImageView statusIv = holder.getView(R.id.status_iv);
        TextView freight_unit_hint = holder.getView(R.id.freight_unit_hint);
        TextView offer_invalid_hint = holder.getView(R.id.offer_invalid_hint);
        TextView offer_invalid = holder.getView(R.id.offer_invalid);
        LinearLayout freight_all_layout = holder.getView(R.id.freight_all_layout);
        LinearLayout freight_layout = holder.getView(R.id.freight_layout);
        TextView is_normal = holder.getView(R.id.is_normal);

        String addStr = "";
        if (!TextUtils.isEmpty(entity.getCityName())) {
            addStr += entity.getCityName() + " ";
        }
        if (!TextUtils.isEmpty(entity.getDistrictName())) {
            addStr += entity.getDistrictName() + " → ";
        } else {
            addStr += " → ";
        }
        if (!TextUtils.isEmpty(entity.getDestCityName())) {
            addStr += entity.getDestCityName() + " ";
        }
        if (!TextUtils.isEmpty(entity.getDestDistrictName())) {
            addStr += entity.getDestDistrictName();
        }
        route_address.setText(addStr);

        if (!TextUtils.isEmpty(entity.getTotalDistance())) {
            String journey = Double.valueOf(entity.getTotalDistance()) < 1000
                    ? "1km以内" : StringUtil.fromatUpDistance(entity.getTotalDistance()) + "km";
            haul.setText("运程:" + journey);
        }

        String goodsInfo = "";
        goodsInfo += CargoConstant.getLastGoodsTypeName(entity.getGoodsTypeName(), entity.getSecondGoodsTypeName()) + "/";
        if (!TextUtils.isEmpty(entity.getWeight())) {
            goodsInfo += NumberUtil.trim0(entity.getWeight()) + "吨/";
        }
        if (!TextUtils.isEmpty(entity.getVolume())) {
            goodsInfo += NumberUtil.trim0(entity.getVolume()) + "方";
        }
        goods_details.setText(StringUtil.removeChar(goodsInfo, "/"));

        if (!TextUtils.isEmpty(entity.getTelphone())) {
            phone_num.setText(entity.getTelphone());
        } else
            phone_num.setText("/");


        if (!TextUtils.isEmpty(entity.getOfferType())) {
            switch (entity.getOfferType()) {
                case "1"://企业
                    name.setText(TextUtils.isEmpty(entity.getCompanyName()) ? "" : entity.getCompanyName() + " ");
                    car_num.setVisibility(View.GONE);
                    car_type.setVisibility(View.GONE);
                    car_type_hint.setVisibility(View.GONE);
                    break;

                case "2"://团队
                    name.setText(TextUtils.isEmpty(entity.getTeamName()) ? "" : entity.getTeamName() + " ");
                    car_num.setVisibility(View.GONE);
                    car_type.setVisibility(View.GONE);
                    car_type_hint.setVisibility(View.GONE);
                    break;

                case "3"://个人
                    name.setText(TextUtils.isEmpty(entity.getName()) ? "" : entity.getName());
                    car_num.setText(TextUtils.isEmpty(entity.getCarNumber()) ? "" : entity.getCarNumber());
                    car_num.setVisibility(View.VISIBLE);
                    String carInfo = "";
                    if (!TextUtils.isEmpty(entity.getCarTypeName())) {
                        carInfo += entity.getCarTypeName() + " ";
                    }
                    if (!TextUtils.isEmpty(entity.getCarLenName())) {
                        carInfo += "车长" + entity.getCarLenName();
                    }
                    car_type.setText(TextUtils.isEmpty(carInfo) ? "/" : carInfo);
                    car_type.setVisibility(View.VISIBLE);
                    car_type_hint.setVisibility(View.VISIBLE);
                    break;
            }
        }
        //0:普通模式  1:竞价模式
        if (entity.getPattern() != null && entity.getPattern() == 1) {
            is_normal.setVisibility(View.VISIBLE);
            is_normal.setText("竞价");
            is_normal.setBackgroundResource(R.drawable.member_shape_gradient_1fd2ac2);

//            if (!TextUtils.isEmpty(entity.getAllFreight())) {
//                freight_all_layout.setVisibility(View.GONE);
//                freight_offer.setText(NumberUtil.trim0(entity.getFreight()) + entity.getFreightUnit());
//               /* if (!TextUtils.isEmpty(entity.getSourceAllFreight()) && !TextUtils.isEmpty(entity.getAllFreight())) {
//                    freight_all.setTextColor(Double.parseDouble(entity.getAllFreight()) > Double.parseDouble(entity.getSourceAllFreight())
//                            ? context.getResources().getColor(R.color.color_FF4B10) : context.getResources().getColor(R.color.color_secondary_yellow));
//                }*/
//            } else {
//                freight_offer.setText("/");
//            }
//
//            freight_unit.setText(entity.getFreightType() == null || entity.getFreightType() == 0 ?
//                    !TextUtils.isEmpty(entity.getSourceFreight()) ? NumberUtil.trim0(entity.getSourceFreight()) + entity.getFreightUnit() : "/" : "电议");
        } else {
            is_normal.setVisibility(View.GONE);
//            is_normal.setBackgroundResource(R.drawable.shape_blue_button);
        }

        freight_all_layout.setVisibility(View.VISIBLE);
        freight_all.setText(NumberUtil.trim0(entity.getAllFreight()) + "元");

        if (TextUtils.isEmpty(entity.getFreight())) {
            freight_offer.setText("/");
        } else {
            freight_offer.setText(NumberUtil.trim0(entity.getFreight()) + entity.getQuotationfreightUnit());
        }

        if (!TextUtils.isEmpty(entity.getSourceMessagePrice())) {
            if ("0".equals(NumberUtil.trim0(entity.getSourceMessagePrice()))) {
                freight_layout.setVisibility(View.GONE);
            } else {
                freight_layout.setVisibility(View.VISIBLE);
                freight_unit_hint.setText("信息费");
                freight_unit.setText(!TextUtils.isEmpty(entity.getSourceMessagePrice()) ? NumberUtil.trim0(entity.getSourceMessagePrice()) + "元" : "/");
            }
        }

        freight.setText(entity.getFreightType() == null || entity.getFreightType() == 0 ?
                !TextUtils.isEmpty(entity.getSourceAllFreight()) ? NumberUtil.trim0(entity.getSourceAllFreight()) + "元" : "/" : "电议");
       /* if (!TextUtils.isEmpty(entity.getSourceFreight())) {
            freight_unit.setText(NumberUtil.trim0(entity.getSourceFreight()) + entity.getFreightUnit());
        } else {
            freight_unit.setText("/");
        }
        if (!TextUtils.isEmpty(entity.getSourceAllFreight())) {
            freight.setText(NumberUtil.trim0(entity.getSourceAllFreight()) + "元");
        } else {
            freight.setText("/");
        }*/

        if (entity.getCreateTime() > 0) {
            offer_time.setText(DateUtils.formatDate(String.valueOf(entity.getCreateTime()), "yyyy-MM-dd HH:mm:ss"));
        } else {
            offer_time.setText("/");
        }

        //报价状态：2.0.8  报价状态：offerStatus     1已报价  2已同意  5货主未同意
        statusIv.setVisibility(View.VISIBLE);
        offer_invalid.setVisibility(View.GONE);
        offer_invalid_hint.setVisibility(View.GONE);
        switch (entity.getOfferStatus()) {
            case 1://已报价
                statusIv.setImageResource(R.mipmap.icon_offer_tobe_confirmed);
                break;

            case 2://已同意
                statusIv.setImageResource(entity.getPattern() == 0 ? R.mipmap.offer_to_pay : R.mipmap.icon_offer_agreed);
                break;

            case 5://未同意
                statusIv.setImageDrawable(entity.getPattern() == 0 ? context.getResources().getDrawable(R.mipmap.offer_off_time) : context.getResources().getDrawable(R.mipmap.icon_offer_disagree_02));
                if (entity.getPattern() == 0) {
                    offer_invalid.setVisibility(entity.getFailureCause() == null ? View.GONE : View.VISIBLE);
                    offer_invalid_hint.setVisibility(entity.getFailureCause() == null ? View.GONE : View.VISIBLE);
                    offer_invalid.setText(entity.getFailureCauseWorlds());
                }
                break;
            case 3://已支付
                statusIv.setImageResource(R.mipmap.offer_payed);
                break;
            case 4:
                if (
                        entity.getPattern() == 0) {
                    offer_invalid.setVisibility(entity.getFailureCause() == null ? View.GONE : View.VISIBLE);
                    offer_invalid_hint.setVisibility(entity.getFailureCause() == null ? View.GONE : View.VISIBLE);
                    offer_invalid.setText(entity.getFailureCauseWorlds());
                }

                statusIv.setImageResource(R.mipmap.offer_off_time);
                break;
            default:
                statusIv.setVisibility(View.GONE);
                break;
        }

    }
}
